package com.rfs.suanfa;

import java.util.ArrayList;
import java.util.HashSet;

public class 二分查找 {
    public static void main(String[] args) {
        //产生1000个随机数
        HashSet<Integer> hs = new HashSet<>();
        while (hs.size() < 1000) {
            int v = (int) (Math.random() * 2000);
            hs.add(v);
        }
        ArrayList<Integer> al = new ArrayList<>(hs);
        System.out.println(al);
        System.out.println(al.size());
        int des = 876;
        int minIndex = 0;
        int maxIndex = 999;
        while (true) {
            int midIndex = (minIndex + maxIndex) / 2;
            if (des == al.get(midIndex)) {
                System.out.println("找到了,第"+midIndex+"个数");
                break;
            }
            if (des<al.get(midIndex)){
                maxIndex=midIndex-1;
            }
            if (des>al.get(midIndex)){
                minIndex=midIndex+1;
            }
            if (minIndex>maxIndex){
                System.out.println("没找到");
                break;
            }
        }
    }
}
